<%--
  Created by IntelliJ IDEA.
  User: jiang
  Date: 2021/7/22
  Time: 10:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>车辆管理</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/component/pear/css/pear.css"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">车牌号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="num" placeholder="" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">车辆类型</label>
                    <div class="layui-input-inline">
                        <select name="type">
                            <option value="">请选择</option>
                            <option value="1">轿车</option>
                            <option value="2">SUV</option>
                            <option value="3">跑车</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">租车价格</label>
                    <div class="layui-input-inline" style="width: 200px;">
                        <input type="number" name="rentPrice" placeholder="￥" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">是否出租</label>
                    <div class="layui-input-inline">
                        <input type="radio" name="isRent" class="layui-input" title="是" value="1">
                        <input type="radio" name="isRent" class="layui-input" title="否" value="0">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="car-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="car-table" lay-filter="car-table"></table>
    </div>
</div>
</body>
<script type="text/html" id="car-toolbar">
    <shiro:hasPermission name="car:add">
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            新增
        </button>
    </shiro:hasPermission>
    <shiro:hasPermission name="car:delete">
        <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
    </shiro:hasPermission>
</script>

<script type="text/html" id="car-bar">
    <shiro:hasPermission name="car:update">
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i
                class="layui-icon layui-icon-edit"></i>
        </button>
    </shiro:hasPermission>
    <shiro:hasPermission name="car:delete">
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
                class="layui-icon layui-icon-delete"></i>
        </button>
    </shiro:hasPermission>
    {{#if (d.isRent == 0) { }}
    <button class="pear-btn pear-btn-success pear-btn-sm" lay-event="rent"><i
            class="layui-icon layui-icon-diamond"></i>
    </button>
    {{# } }}

</script>

<script type="text/html" id="car-enable">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="car-enable" {{
           d.deleted== 1 ? 'checked': ''}}>
</script>

<script type="text/html" id="car-type">
    {{#if (d.type == 1) { }}
    <span>轿车</span>
    {{# }else if(d.type == 2){ }}
    <span>SUV</span>
    {{# }else if(d.type == 3){ }}
    <span>跑车</span>
    {{# } }}
</script>

<script type="text/html" id="car-isRent">
    {{#if (d.isRent == 0) { }}
    <span class="layui-bg-green layui-btn-radius">未出租</span>
    {{# }else if(d.isRent == 1){ }}
    <span class="layui-bg-red layui-btn-radius">已出租</span>
    {{# } }}
</script>

<script type="text/html" id="car-createTime">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}
</script>

<script type="text/html" id="car-form-add" lay-filter="car-form-add">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" action="" lay-filter="addCarFrm">
                <div class="layui-form-item">
                    <label class="layui-form-label">车牌号</label>
                    <div class="layui-input-block">
                        <input type="text" name="num" placeholder="请输入车牌号" class="layui-input"
                               lay-verify="required"/>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">车辆的类型</label>
                    <div class="layui-input-block">
                        <select name="type">
                            <option value="1">轿车</option>
                            <option value="2">SUV</option>
                            <option value="3">跑车</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">颜色</label>
                    <div class="layui-input-block" style="width: 100px">
                        <input type="color" name="color" class="layui-input">
                    </div>
                </div>


                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">车辆价格</label>
                        <div class="layui-input-inline" style="width: 100px;">
                            <input type="number" name="price" placeholder="￥" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">租车价格</label>
                        <div class="layui-input-inline" style="width: 100px;">
                            <input type="number" name="rentPrice" placeholder="￥" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">押金</label>
                        <div class="layui-input-inline" style="width: 100px;">
                            <input type="number" name="deposit" placeholder="￥" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">车辆图片</label>
                    <div class="layui-input-block">
                        <input type="text" name="img" lay-verify="title" autocomplete="off" placeholder=""
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">车辆描述</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入对车辆的描述" name="descp" class="layui-textarea"></textarea>
                    </div>
                </div>

                <div class="layui-form-item" style="text-align: center">
                    <div class="layui-inline">
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="car-add">
                            <i class="layui-icon">&#xe605;</i>
                            确认
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md" lay-submit>
                            <i class="layui-icon layui-icon-refresh"></i>
                            取消
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</script>

<script type="text/html" id="car-form-update">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" lay-filter="updateCarFrm" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">车牌号</label>
                    <div class="layui-input-block">
                        <input type="hidden" name="id" value="">
                        <input type="text" name="num" placeholder="请输入车牌号" class="layui-input"
                               lay-verify="required" readonly/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">车辆类型</label>
                    <div class="layui-input-block">
                        <input name="type" class="layui-input" type="radio" value="1" title="轿车">
                        <input name="type" class="layui-input" type="radio" value="2" title="SUV">
                        <input name="type" class="layui-input" type="radio" value="3" title="跑车">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">车辆颜色</label>
                    <div class="layui-input-block" style="width:100px">
                        <input type="color" name="color" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">车辆价格</label>
                        <div class="layui-input-inline" style="width: 100px;">
                            <input type="number" name="price" placeholder="￥" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">租车价格</label>
                        <div class="layui-input-inline" style="width: 100px;">
                            <input type="number" name="rentPrice" placeholder="￥" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">押金</label>
                        <div class="layui-input-inline" style="width: 100px;">
                            <input type="number" name="deposit" placeholder="￥" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">是否出租</label>
                    <div class="layui-input-block">
                        <input name="isRent" class="layui-input" type="radio" value="0" title="已出租">
                        <input name="isRent" class="layui-input" type="radio" value="1" title="未出租">
                    </div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">车辆图片</label>
                    <div class="layui-input-block">
                        <input type="text" name="img" lay-verify="title" autocomplete="off" placeholder=""
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">车辆描述</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入对车辆的描述" name="descp" class="layui-textarea"></textarea>
                    </div>
                </div>

                <div class="layui-form-item" style="text-align: center">
                    <div class="layui-inline">
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="car-update">
                            <i class="layui-icon">&#xe605;</i>
                            确认
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md">
                            <i class="layui-icon layui-icon-refresh"></i>
                            取消
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</script>

<script type="text/html" id="car-form-rent">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" lay-filter="rentCarFrm" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">车牌号</label>
                    <div class="layui-input-block">
                        <input type="hidden" name="id" value="">
                        <input type="text" name="num" placeholder="" class="layui-input"
                               lay-verify="required" readonly/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">车辆类型</label>
                    <div class="layui-input-block">
                        <select name="type" disabled>
                            <option value="1">轿车</option>
                            <option value="2">SUV</option>
                            <option value="3">跑车</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">车辆租金</label>
                    <div class="layui-input-block">
                        <input type="text" name="rentPrice" placeholder="" class="layui-input"
                               lay-verify="required" readonly/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">选择客户</label>
                    <div class="layui-input-inline">
                        <input id="customer_id" name="customerId" type="hidden">
                        <input type="text" name="customerName" id="customerDropDown" class="layui-input"
                               lay-verify="required" readonly/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">租车时间</label>
                        <div class="layui-inline" id="rentTimeScope">
                            <div class="layui-input-inline">
                                <input type="text" autocomplete="off" id="startDateTime" name="startTime"
                                       class="layui-input" placeholder="租车开始时间" readonly>
                            </div>
                            <div class="layui-form-mid">-</div>
                            <div class="layui-input-inline">
                                <input type="text" autocomplete="off" id="endDateTime" name="endTime"
                                       class="layui-input" placeholder="租车结束日期" readonly>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item" style="text-align: center">
                    <div class="layui-inline">
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="car-rent">
                            <i class="layui-icon">&#xe605;</i>
                            确认
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md">
                            <i class="layui-icon layui-icon-refresh"></i>
                            取消
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</script>

<script src="${pageContext.request.contextPath}/resources/component/layui/layui.js"></script>
<script src="${pageContext.request.contextPath}/resources/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'dropdown', 'laydate'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let dropdown = layui.dropdown;
        let laydate = layui.laydate;

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '车牌号',
                    field: 'num',
                    align: 'center',
                    width: 100
                },
                {
                    title: '车辆类型',
                    field: 'type',
                    align: 'center',
                    templet: '#car-type'
                },
                {
                    title: '车辆颜色',
                    field: 'color',
                    align: 'center',
                },
                {
                    title: '车辆价格',
                    field: 'price',
                    align: 'center'
                },
                {
                    title: '租赁价格',
                    field: 'rentPrice',
                    align: 'center'
                },
                {
                    title: '押金',
                    field: 'deposit',
                    align: 'center'
                },
                {
                    title: '是否出租',
                    field: 'isRent',
                    align: 'center',
                    templet: '#car-isRent'
                },
                {
                    title: '车辆图片',
                    field: 'img',
                    align: 'center'
                },
                {
                    title: '车辆描述',
                    field: 'descp',
                    align: 'center'
                },
                {
                    title: '创建时间',
                    field: 'createTime',
                    align: 'center',
                    templet: '#car-createTime'
                },
                {
                    title: '操作',
                    toolbar: '#car-bar',
                    align: 'center',
                    width: 150
                }
            ]
        ]

        table.render({
            elem: '#car-table',
            url: '${pageContext.request.contextPath}/car/findByPage.do',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#car-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
            , parseData: function (res) {
                //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.list //解析数据列表
                };
            }
            , response: {

                statusName: 'code' //规定数据状态的字段名称，默认：code
                , statusCode: 200 //规定成功的状态码，默认：0
                , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                , dataName: 'data' //规定数据列表的字段名称，默认：data
            }
        });

        table.on('tool(car-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'rent') {
                window.rent(obj);
            }
        });

        table.on('toolbar(car-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(car-query)', function (data) {
            table.reload('car-table', {
                where: data.field
            })
            return false;
        });

        form.on('switch(car-enable)', function (obj) {
            $.post("${pageContext.request.contextPath}/car/enable.do?id=" + obj.value, function (res) {
                if (res.code == 200) {
                    table.reload('car-table');
                }
                layer.msg(res.msg)
            });
        });


        let mainIndex;

        window.add = function () {
            mainIndex = layer.open({
                type: 1,
                title: '新增车辆',
                shade: 0.1,
                area: ['800px', '600px'],
                content: $("#car-form-add").html(),
                success: function (layero, index) {
                    // 渲染表单的元素
                    form.render('select');
                }
            });
        }

        window.edit = function (obj) {
            mainIndex = layer.open({
                type: 1,
                title: '修改车辆',
                shade: 0.1,
                area: ['1000px', '600px'],
                content: $("#car-form-update").html(),
                success: function (layero, index) {
                    console.log(obj.data);
                    form.val("updateCarFrm", obj.data);
                    form.render('radio');
                }
            });
        }

        window.rent = function (obj) {
            mainIndex = layer.open({
                type: 1,
                title: '出租车辆',
                shade: 0.1,
                area: ['800px', '600px'],
                content: $("#car-form-rent").html(),
                success: function (layero, index) {
                    form.val('rentCarFrm', obj.data);
                    form.render('select');
                    $.get('${pageContext.request.contextPath}/customer/getAllCustomer.do', function (res) {
                        if (res.code == 200) {
                            dropdown.render({
                                elem: '#customerDropDown', //可绑定在任意元素中，此处以上述按钮为例
                                data: res.data,
                                id: 'dropDownDemo1',
                                //菜单被点击的事件
                                click: function (obj) {
                                    $('#customer_id').val(obj.id);
                                    $('#customerDropDown').val(obj.title)
                                }
                            });
                        }
                    });

                    //日期范围
                    laydate.render({
                        elem: '#rentTimeScope',
                        type: 'datetime',
                        //设置开始日期、日期日期的 input 选择器
                        //数组格式为 2.6.6 开始新增，之前版本直接配置 true 或任意分割字符即可
                        range: ['#startDateTime', '#endDateTime']
                    });
                }
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该车辆', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: '${pageContext.request.contextPath}/car/delete.do?id=' + obj.data['id'],
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('car-table')
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }


        window.batchRemove = function (obj) {

            var checkIds = common.checkField(obj, 'id');

            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除这些车辆', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: '${pageContext.request.contextPath}/car/delete.do?id=' + checkIds,
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('car-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('car-table');
        }

        form.on('submit(car-add)', function (obj) {
            console.log(obj);
            $.post("${pageContext.request.contextPath}/car/add.do", obj.field, function (result) {
                if (result.code == 200) {
                    // 新增成功
                    layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                        layer.close(mainIndex);//关闭当前页
                        table.reload("car-table");
                    });
                    return;
                }
                layer.msg(result.msg, {icon: 2});
            })
            return false;
        })

        form.on('submit(car-update)', function (obj) {
            console.log(mainIndex);
            $.post("${pageContext.request.contextPath}/car/update.do", obj.field, function (result) {
                if (result.code == 200) {
                    // 新增成功
                    layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                        layer.close(mainIndex);//关闭当前页
                        table.reload("car-table");
                    });
                    return;
                }
                layer.msg(result.msg, {icon: 2});
            })
            return false;
        });

        form.on('submit(car-rent)', function (obj) {
            let data = {
                carId: obj.field.id,
                customerId: obj.field.customerId,
                beginTime: obj.field.startTime,
                endTime: obj.field.endTime
            };

            $.post("${pageContext.request.contextPath}/car/doRent.do", data, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                        layer.close(mainIndex);//关闭当前页
                        table.reload("car-table");
                    });
                    return;
                }
                layer.msg(res.msg, {icon: 2});
            });
            return false;
        });

        form.on('submit(close)', function () {
            table.reload("car-table");
            layer.close(mainIndex);
            return false;
        });
    })
</script>
</html>

